﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using grounding.grounder;
using grounding.nao;
using grounding.parser;
using grounding.parser.Parsers;
using grounding.nao.sensors;
using grounding.nao.enums;

namespace grounding.tests.SVMTest
{
    class Program
    {
        static void Main(string[] args)
        {
            SVMGrounder grounder = new SVMGrounder(numFeatures: -1);
            Console.WriteLine("Created SVMGrounder with sigma of {0} and complexity of {1}...", grounder.SVMSigma, grounder.Complexity);

            Console.WriteLine("Please enter the dataset to load:");
            TrainingData data = TrainingData.LoadFromDisk(Console.ReadLine());
            Console.WriteLine("Loaded Training Data...");

            grounder.TrainSingle(data);

            Console.Write("Cross Validating... ");
            int correct = 0, incorrect = 0;
            for (int i = 0; i < data.DataVector.Length; i++)
            {
                Dictionary<ILogicElement, double> results = grounder.Test(data.DataVector[i]);
                foreach (ILogicElement label in results.Keys)
                {
                    if (((Label)label).NormalizedIdentifier == data.Classes[i])
                        correct++;
                    else
                        incorrect++;
                }
            }
            Console.WriteLine("c: {0}, i: {1}", correct, incorrect);

            Dictionary<Sensor, Func<IEnumerable<double>, IEnumerable<double>>> transformers = new Dictionary<Sensor, Func<IEnumerable<double>, IEnumerable<double>>>()
            {
                {Sensor.JointCurrentSensor, x => new double[] { x.Average() } },
                {Sensor.FootSensor, x => new double[] { x.Average() } },
                {Sensor.TemperatureSensor, x => new double[] { x.Average() } }
            };

            Nao nao = new Nao("128.208.4.19", 9559, transformers: transformers,
                active: new Sensor[] {
                    Sensor.FootSensor,
                    Sensor.Sonar});
            Console.WriteLine("Connected to Nao...");

            Console.Write("Press any key to classify current sensor frame");
            while (Console.ReadKey(true).Key != ConsoleKey.Escape)
            {
                Dictionary<ILogicElement, double> results = grounder.Test(nao.GetSensorFrame().ToArray());
                Console.WriteLine("Classified as:");
                foreach (ILogicElement category in results.Keys)
                    Console.WriteLine("{0,15} : {1}", category.Element, results[category]);
            }
        }
    }
}
